<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    button{
      position: fixed;
      bottom:50px;
      right:50px;
    }
  </style>
</head>

<body style="height:3000px">
  <div id="app">
    <button v-scroll="500">回到顶部</button>
  </div>

  <script src="./vue.js"></script>
  <script>
    Vue.directive('scroll', {
      inserted(el, binding, vnode) {
        let { value } = binding
        el.style.display="none";
        window.onscroll = function(){
          // 获取滚动距离
          let sTop = document.documentElement.scrollTop||document.body.scrollTop;
          if(sTop>value){
            el.style.display="block";
          }else{
            el.style.display="none";
          }
        }
        el.onclick = function(){
          window.scrollTo(0,0)
        }
      },
      unbind(){
        window.onscroll = null;
      }
    });
    const vm = new Vue({
      el: "#app",
      data: {
        num: 100
      }
    })
  </script>
</body>

</html>
